home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / graphic / jpegsrc4.zip / JDMAIN.C < prev    next >
C/C++ Source or Header  |  1992-11-11  |  14KB  |  474 lines

  1. /*
  2.  * jdmain.c
  3.  *
  4.  * Copyright (C) 1991, 1992, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains a command-line user interface for the JPEG decompressor.
  9.  * It should work on any system with Unix- or MS-DOS-style command lines.
  10.  *
  11.  * Two different command line styles are permitted, depending on the
  12.  * compile-time switch TWO_FILE_COMMANDLINE:
  13.  *    djpeg [options]  inputfile outputfile
  14.  *    djpeg [options]  [inputfile]
  15.  * In the second style, output is always to standard output, which you'd
  16.  * normally redirect to a file or pipe to some other program.  Input is
  17.  * either from a named file or from standard input (typically redirected).
  18.  * The second style is convenient on Unix but is unhelpful on systems that
  19.  * don't support pipes.  Also, you MUST use the first style if your system
  20.  * doesn't do binary I/O to stdin/stdout.
  21.  */
  22.  
  23. #include "jinclude.h"
  24. #ifdef INCLUDES_ARE_ANSI
  25. #include <stdlib.h>        /* to declare exit() */
  26. #endif
  27. #include <ctype.h>        /* to declare isupper(), tolower() */
  28. #ifdef NEED_SIGNAL_CATCHER
  29. #include <signal.h>        /* to declare signal() */
  30. #endif
  31. #ifdef USE_SETMODE
  32. #include <fcntl.h>        /* to declare setmode() */
  33. #endif
  34.  
  35. #ifdef THINK_C
  36. #include <console.h>        /* command-line reader for Macintosh */
  37. #endif
  38.  
  39. #ifdef DONT_USE_B_MODE        /* define mode parameters for fopen() */
  40. #define READ_BINARY    "r"
  41. #define WRITE_BINARY    "w"
  42. #else
  43. #define READ_BINARY    "rb"
  44. #define WRITE_BINARY    "wb"
  45. #endif
  46.  
  47. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  48. #define EXIT_FAILURE  1
  49. #endif
  50. #ifndef EXIT_SUCCESS
  51. #ifdef VMS
  52. #define EXIT_SUCCESS  1        /* VMS is very nonstandard */
  53. #else
  54. #define EXIT_SUCCESS  0
  55. #endif
  56. #endif
  57.  
  58.  
  59. #include "jversion.h"        /* for version message */
  60.  
  61.  
  62. /*
  63.  * This list defines the known output image formats
  64.  * (not all of which need be supported by a given version).
  65.  * You can change the default output format by defining DEFAULT_FMT;
  66.  * indeed, you had better do so if you undefine PPM_SUPPORTED.
  67.  */
  68.  
  69. typedef enum {
  70.     FMT_GIF,        /* GIF format */
  71.     FMT_PPM,        /* PPM/PGM (PBMPLUS formats) */
  72.     FMT_RLE,        /* RLE format */
  73.     FMT_TARGA,        /* Targa format */
  74.     FMT_TIFF        /* TIFF format */
  75. } IMAGE_FORMATS;
  76.  
  77. #ifndef DEFAULT_FMT        /* so can override from CFLAGS in Makefile */
  78. #define DEFAULT_FMT    FMT_PPM
  79. #endif
  80.  
  81. static IMAGE_FORMATS requested_fmt;
  82.  
  83.  
  84. /*
  85.  * This routine gets control after the input file header has been read.
  86.  * It must determine what output file format is to be written,
  87.  * and make any other decompression parameter changes that are desirable.
  88.  */
  89.  
  90. METHODDEF void
  91. d_ui_method_selection (decompress_info_ptr cinfo)
  92. {
  93.   /* if grayscale or CMYK input, force similar output; */
  94.   /* else leave the output colorspace as set by options. */
  95.   if (cinfo->jpeg_color_space == CS_GRAYSCALE)
  96.     cinfo->out_color_space = CS_GRAYSCALE;
  97.   else if (cinfo->jpeg_color_space == CS_CMYK)
  98.     cinfo->out_color_space = CS_CMYK;
  99.  
  100.   /* select output file format */
  101.   /* Note: jselwxxx routine may make additional parameter changes,
  102.    * such as forcing color quantization if it's a colormapped format.
  103.    */
  104.   switch (requested_fmt) {
  105. #ifdef GIF_SUPPORTED
  106.   case FMT_GIF:
  107.     jselwgif(cinfo);
  108.     break;
  109. #endif
  110. #ifdef PPM_SUPPORTED
  111.   case FMT_PPM:
  112.     jselwppm(cinfo);
  113.     break;
  114. #endif
  115. #ifdef RLE_SUPPORTED
  116.   case FMT_RLE:
  117.     jselwrle(cinfo);
  118.     break;
  119. #endif
  120. #ifdef TARGA_SUPPORTED
  121.   case FMT_TARGA:
  122.     jselwtarga(cinfo);
  123.     break;
  124. #endif
  125.   default:
  126.     ERREXIT(cinfo->emethods, "Unsupported output file format");
  127.     break;
  128.   }
  129. }
  130.  
  131.  
  132. /*
  133.  * Signal catcher to ensure that temporary files are removed before aborting.
  134.  * NB: for Amiga Manx C this is actually a global routine named _abort();
  135.  * see -Dsignal_catcher=_abort in CFLAGS.  Talk about bogus...
  136.  */
  137.  
  138. #ifdef NEED_SIGNAL_CATCHER
  139.  
  140. static external_methods_ptr emethods; /* for access to free_all */
  141.  
  142. GLOBAL void
  143. signal_catcher (int signum)
  144. {
  145.   if (emethods != NULL) {
  146.     emethods->trace_level = 0;    /* turn off trace output */
  147.     (*emethods->free_all) ();    /* clean up memory allocation & temp files */
  148.   }
  149.   exit(EXIT_FAILURE);
  150. }
  151.  
  152. #endif
  153.  
  154.  
  155. /*
  156.  * Optional routine to display a percent-done figure on stderr.
  157.  * See jddeflts.c for explanation of the information used.
  158.  */
  159.  
  160. #ifdef PROGRESS_REPORT
  161.  
  162. METHODDEF void
  163. progress_monitor (decompress_info_ptr cinfo, long loopcounter, long looplimit)
  164. {
  165.   if (cinfo->total_passes > 1) {
  166.     fprintf(stderr, "\rPass %d/%d: %3d%% ",
  167.         cinfo->completed_passes+1, cinfo->total_passes,
  168.         (int) (loopcounter*100L/looplimit));
  169.   } else {
  170.     fprintf(stderr, "\r %3d%% ",
  171.         (int) (loopcounter*100L/looplimit));
  172.   }
  173.   fflush(stderr);
  174. }
  175.  
  176. #endif
  177.  
  178.  
  179. /*
  180.  * Argument-parsing code.
  181.  * The switch parser is designed to be useful with DOS-style command line
  182.  * syntax, ie, intermixed switches and file names, where only the switches
  183.  * to the left of a given file name affect processing of that file.
  184.  * The main program in this file doesn't actually use this capability...
  185.  */
  186.  
  187.  
  188. static char * progname;        /* program name for error messages */
  189.  
  190.  
  191. LOCAL void
  192. usage (void)
  193. /* complain about bad command line */
  194. {
  195.   fprintf(stderr, "usage: %s [switches] ", progname);
  196. #ifdef TWO_FILE_COMMANDLINE
  197.   fprintf(stderr, "inputfile outputfile\n");
  198. #else
  199.   fprintf(stderr, "[inputfile]\n");
  200. #endif
  201.  
  202.   fprintf(stderr, "Switches (names may be abbreviated):\n");
  203.   fprintf(stderr, "  -colors N      Reduce image to no more than N colors\n");
  204. #ifdef GIF_SUPPORTED
  205.   fprintf(stderr, "  -gif           Select GIF output format\n");
  206. #endif
  207. #ifdef PPM_SUPPORTED
  208.   fprintf(stderr, "  -pnm           Select PBMPLUS (PPM/PGM) output format (default)\n");
  209. #endif
  210.   fprintf(stderr, "  -quantize N    Same as -colors N\n");
  211. #ifdef RLE_SUPPORTED
  212.   fprintf(stderr, "  -rle           Select Utah RLE output format\n");
  213. #endif
  214. #ifdef TARGA_SUPPORTED
  215.   fprintf(stderr, "  -targa         Select Targa output format\n");
  216. #endif
  217.   fprintf(stderr, "Switches for advanced users:\n");
  218. #ifdef BLOCK_SMOOTHING_SUPPORTED
  219.   fprintf(stderr, "  -blocksmooth   Apply cross-block smoothing\n");
  220. #endif
  221.   fprintf(stderr, "  -grayscale     Force grayscale output\n");
  222.   fprintf(stderr, "  -nodither      Don't use dithering in quantization\n");
  223. #ifdef QUANT_1PASS_SUPPORTED
  224.   fprintf(stderr, "  -onepass       Use 1-pass quantization (fast, low quality)\n");
  225. #endif
  226.   fprintf(stderr, "  -maxmemory N   Maximum memory to use (in kbytes)\n");
  227.   fprintf(stderr, "  -verbose  or  -debug   Emit debug output\n");
  228.   exit(EXIT_FAILURE);
  229. }
  230.  
  231.  
  232. LOCAL boolean
  233. keymatch (char * arg, const char * keyword, int minchars)
  234. /* Case-insensitive matching of (possibly abbreviated) keyword switches. */
  235. /* keyword is the constant keyword (must be lower case already), */
  236. /* minchars is length of minimum legal abbreviation. */
  237. {
  238.   register int ca, ck;
  239.   register int nmatched = 0;
  240.  
  241.   while ((ca = *arg++) != '\0') {
  242.     if ((ck = *keyword++) == '\0')
  243.       return FALSE;        /* arg longer than keyword, no good */
  244.     if (isupper(ca))        /* force arg to lcase (assume ck is already) */
  245.       ca = tolower(ca);
  246.     if (ca != ck)
  247.       return FALSE;        /* no good */
  248.     nmatched++;            /* count matched characters */
  249.   }
  250.   /* reached end of argument; fail if it's too short for unique abbrev */
  251.   if (nmatched < minchars)
  252.     return FALSE;
  253.   return TRUE;            /* A-OK */
  254. }
  255.  
  256.  
  257. LOCAL int
  258. parse_switches (decompress_info_ptr cinfo, int last_file_arg_seen,
  259.         int argc, char **argv)
  260. /* Initialize cinfo with default switch settings, then parse option switches.
  261.  * Returns argv[] index of first file-name argument (== argc if none).
  262.  * Any file names with indexes <= last_file_arg_seen are ignored;
  263.  * they have presumably been processed in a previous iteration.
  264.  * (Pass 0 for last_file_arg_seen on the first or only iteration.)
  265.  */
  266. {
  267.   int argn;
  268.   char * arg;
  269.  
  270.   /* (Re-)initialize the system-dependent error and memory managers. */
  271.   jselerror(cinfo->emethods);    /* er